<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\File;

class Im extends Common
{

    //设置Socket
    public function setSocket()
    {
        if (request()->isAjax()) {

            $file = request()->file('file');

            if($file){
                $info = $file->move(ROOT_PATH . 'ssl/','');
                if($info){
                    if($info->getExtension() == 'key'){
                        // 成功上传后 获取上传信息
                        $data=[];
                        $data['file_name'] = $info->getFilename();
                        $data['path'] = ROOT_PATH . 'ssl/'.$info->getFilename();
                        datamsg(200, '密钥上传成功', $data);
                    }elseif ($info->getExtension() == 'pem'){
                        $data=[];
                        $data['file_name'] = $info->getFilename();
                        $data['path'] = ROOT_PATH . 'ssl/'.$info->getFilename();
                        datamsg(200, '证书上传成功', $data);
                    }
                }else{
                    // 上传失败获取错误信息
                    datamsg(400, '上传失败');
                }
            }
        } else {
            $fileKey = file_exists(ROOT_PATH . 'ssl/private.key');
            if($fileKey){
                $this->assign('file_key','private.key');
            }
            $filePem = file_exists(ROOT_PATH . 'ssl/full_chain.pem');
            if($filePem){
                $this->assign('file_pem','full_chain.pem');
            }
            return $this->fetch();
        }
    }

}